package cn.xuewenbao.edu.service.impl;

import cn.xuewenbao.edu.entity.WeekLearnTime;
import cn.xuewenbao.edu.entity.vo.WeekLearnTimeVO;
import cn.xuewenbao.edu.mapper.WeekLearnTimeMapper;
import cn.xuewenbao.edu.service.WeekLearnTimeService;
import cn.xuewenbao.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class WeekLearnTimeServiceImpl extends ServiceImpl<WeekLearnTimeMapper, WeekLearnTime> implements WeekLearnTimeService {

    @Autowired
    private MemberService memberService;

    @Override
    public Result<?> updateWeekLearnTime(WeekLearnTime weekLearnTime) {
        //参数检查
        if (weekLearnTime == null) {
            return Result.error("新的每周学习时间信息为空");
        }
        Double time = weekLearnTime.getWeekLearnTime();
        String userId = weekLearnTime.getUserId();
        if (time == null || userId == null) {
            return Result.error("每周学习时间参数不完整");
        }

        //查询是否已有记录
        Calendar today = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));   //当前日期
        int year = today.getWeekYear(); //当前年
        int weekOfYear = today.getWeeksInWeekYear(); //年中第几周
        QueryWrapper<WeekLearnTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("year", year).eq("week_of_year", weekOfYear);
        WeekLearnTime queryTime = baseMapper.selectOne(queryWrapper);

        //没有记录, 添加
        if (queryTime == null) {
            weekLearnTime.setYear(year);
            weekLearnTime.setWeekOfYear(weekOfYear);
            int insert = baseMapper.insert(weekLearnTime);
            if (insert < 1) {
                return Result.error("新的每周学习时间记录添加失败");
            }
            return Result.OK("新的每周学习时间记录添加成功");
        }

        //本周已有学习时间记录, 更新
        UpdateWrapper<WeekLearnTime> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("week_learn_time", queryTime.getWeekLearnTime() + weekLearnTime.getWeekLearnTime())
                .set("gmt_modified", new Date());
        updateWrapper.eq("user_id", userId).eq("year", year).eq("week_of_year", weekOfYear);
        int update = baseMapper.update(null, updateWrapper);
        if (update < 1) {
            return Result.error("每周学习时间记录更新失败");
        }
        return Result.OK("每周学习时间记录更新成功");
    }

    @Override
    public Result<?> getWeekLearnTimeVO(String userId) {
        WeekLearnTimeVO vo = new WeekLearnTimeVO();
        vo.setUserId(userId);

        Integer totalUserNumber = memberService.getTotalUserNumber();
        vo.setTotalUserNum(totalUserNumber);

        Calendar today = Calendar.getInstance();
        int year = today.getWeekYear();
        int weekOfYear = today.getWeeksInWeekYear();
        QueryWrapper<WeekLearnTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("year", year).eq("week_of_year", weekOfYear);
        WeekLearnTime weekLearnTime = baseMapper.selectOne(queryWrapper);

        if (weekLearnTime == null) {
            vo.setOverUserNum(totalUserNumber);
            vo.setWeekLearnTime(0.0);
            return Result.OK(vo);
        }

        Double time = weekLearnTime.getWeekLearnTime();
        queryWrapper.clear();
        queryWrapper.eq("year", year).eq("week_of_year", weekOfYear);
        queryWrapper.gt("week_learn_time", time);
        Integer overUserNumber = baseMapper.selectCount(queryWrapper);

        vo.setWeekLearnTime(weekLearnTime.getWeekLearnTime());
        vo.setOverUserNum(overUserNumber);
        return Result.OK(vo);
    }

}
